package eai.tp2.lpco.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eai.tp2.lpco.web.beans.Cart;
import eai.tp2.lpco.web.beans.ErrorList;

/**
 * Servlet implementation class CartOrder
 */
public class CartOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartOrder() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String action = request.getParameter("do");
		String name = request.getParameter("name");
		if(action.equals("remove")) {
			Cart cart = (Cart) session.getAttribute("cart");
			cart.removeCameraToCart(name);
			response.sendRedirect("DatabaseCatalog?do=detail&name="+name);	
		}else if(action.equals("add")) {
			Cart cart = (Cart) session.getAttribute("cart");
			cart.addCameraToCart(name);
			response.sendRedirect("DatabaseCatalog?do=detail&name="+name);	
		} else {
			String error;
			error = "Action not recognized.";
			List<String> errors = new ArrayList<String>();
			errors.add(error);
			ErrorList list = new ErrorList();
			list.setErrorList(errors);		
			request.setAttribute("list", list);
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);	
		}
	}

}
